//
//  UIImage+NXAdd.m
//  NCube
//
//  Created by 郑佳 on 17/1/22.
//  Copyright © 2017年 junjie.liu. All rights reserved.
//

#import "UIImage+NXAdd.h"

@implementation UIImage (NXAdd)

+ (void)nx_drawCornerWithImage:(UIImage *)image size:(CGSize)size finishBlock:(void (^)(UIImage *))finishBlock {
    
    CGFloat side = MIN(size.width, size.height);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(side, side), false, [UIScreen mainScreen].scale);
        CGContextAddPath(UIGraphicsGetCurrentContext(),
                         [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, side, side)].CGPath);
        CGContextClip(UIGraphicsGetCurrentContext());
        CGFloat marginX = -(size.width - side) / 2.f;
        CGFloat marginY = -(size.height - side) / 2.f;
        [image drawInRect:CGRectMake(marginX, marginY, size.width, size.height)];
        CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        if (finishBlock) {
            finishBlock(image);
        }
    });
}

@end
